package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.param.User;
import com.barbeque.mall.bean.system.MarketAdmin;
import com.barbeque.mall.bean.system.MarketAdminExample;
import com.barbeque.mall.bean.system.MarketRole;
import com.barbeque.mall.bean.system.MarketRoleExample;
import com.barbeque.mall.bean.system.vo.CreateAdminVO;
import com.barbeque.mall.bean.system.vo.MarketRoleVO;
import com.barbeque.mall.mapper.MarketAdminMapper;
import com.barbeque.mall.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/15 23:50
 */
@Service
public class SysAdminServerImpl implements SysAdminServer {

    @Autowired
    MarketAdminMapper marketAdminMapper;



    /**
     * 查询管理员列表,只查询删除为不为1的值
     * 当username不为空时，根据username进行模糊查询
     * @param basePageInfo
     * @param username
     * @return java.util.List<com.barbeque.mall.bean.system.MarketAdmin>
     * @author 苏东宇
     * @since 2022/07/15 23:53
     */
    @Override
    public CommonData<MarketAdmin> getAdminList(BasePageInfo basePageInfo, String username) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (username != null){
            criteria.andUsernameLike("%"+username+"%");
        }

        // 将查询结果作为有参构造方法的形参传入，可以获得PageInfo
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        PageInfo<MarketAdmin> marketAdminPageInfo = new PageInfo<>(marketAdmins);

        return CommonData.data(marketAdminPageInfo);
    }

 //    /**
 //     * 根据用户名模糊查找
 // * @param basePageInfo
 // * @param username
 //     * @return com.barbeque.mall.bean.param.CommonData
 //     * @author 苏东宇
 //     * @since 2022/07/17 21:42
 //     */
 //    @Override
 //    public CommonData getAdminListByName(BasePageInfo basePageInfo, String username) {
 //        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
 //
 //        MarketAdminExample marketAdminExample = new MarketAdminExample();
 //        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
 //        criteria.andUsernameLike("%"+username+"%");
 //        criteria.andDeletedEqualTo(false);
 //
 //        return null;
 //    }


    /**
     * 添加管理员
     * @param marketAdmin
     * @return int
     * @author 苏东宇
     * @since 2022/07/16 22:31
     */
    @Override
    public int createAdmin(MarketAdmin marketAdmin) {
        int i = marketAdminMapper.insertSelective(marketAdmin);
        //添加完成后可以获得管理员的id，根据id，查询信息
        Integer id = marketAdmin.getId();


        return i;
    }

    /**
     * 根据管理员id获取管理员信息 
 * @param id 
     * @return com.barbeque.mall.bean.system.vo.CreateAdminVO
     * @author 苏东宇
     * @since 2022/07/17 11:33 
     */
    @Override
    public CreateAdminVO getAdminVOById(int id) {

        // MarketAdminExample marketAdminExample = new MarketAdminExample();
        // MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        // criteria.andIdEqualTo(id);

        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(id);
        CreateAdminVO createAdminVO = new CreateAdminVO();

        BeanUtils.copyProperties(marketAdmin,createAdminVO);

        return createAdminVO;
    }

    /**
     * 修改管理员信息
 * @param marketAdmin
     * @return int
     * @author 苏东宇
     * @since 2022/07/17 20:25
     */
    @Override
    public int updateAdmin(MarketAdmin marketAdmin) {

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andIdEqualTo(marketAdmin.getId());
        int i = marketAdminMapper.updateByExampleSelective(marketAdmin,marketAdminExample);
        return i;
    }

    @Override
    public int deleteAdmin(MarketAdmin marketAdmin) {

        return 0;
    }

    /**
     * 根据用户名查询用户
 * @param username
     * @return java.util.List<com.barbeque.mall.bean.system.MarketAdmin>
     * @author 苏东宇
     * @since 2022/07/21 21:17
     */
    @Override
    public List<MarketAdmin> getAdminByUsername(String username) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);


        return marketAdmins;
    }

}
